package war2100.editor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

import war2100.Editor;
import war2100.GameConstants;
import war2100.entity.Map;
import war2100.entity.help.BasicHelpEntity;
import war2100.entity.help.PlayerStartPos;
import war2100.entity.tiles.Tile;

public class MapDisplay extends JPanel implements MouseListener, MouseMotionListener {
	private static final long serialVersionUID = 1636355928387395676L;

	private Map map;
	private Editor editor;

	public MapDisplay(Map map, Editor editor) {
		super();
		this.map = map;
		this.editor = editor;
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}

	public MapDisplay(Editor editor) {
		this(null, editor);
	}

	public void paint(Graphics g) {
		super.paint(g);

		int paintWidth = this.getWidth();
		int paintHeight = this.getHeight();

		g.setColor(Color.white);
		g.fillRect(0, 0, paintWidth, paintHeight);

		if (map != null) {
			int fieldWidth = GameConstants.tileXSize;
			int fieldHeight = GameConstants.tileYSize;

			if (fieldWidth > fieldHeight)
				fieldWidth = fieldHeight;
			else
				fieldHeight = fieldWidth;

			for (int x = 0; x < map.getxSize(); x++) {
				for (int y = 0; y < map.getySize(); y++) {
					int xPos = (fieldWidth * x);
					int yPos = (fieldHeight * y);
					map.getField(x, y).render(g, xPos, yPos, fieldWidth, fieldHeight);
					g.drawRect(xPos, yPos, fieldWidth, fieldHeight);
				}
			}

			for (BasicHelpEntity entity : map.getEntitys()) {
				if (entity == null) {
					break;
				}
				int x = (int) (fieldWidth * entity.getX());
				int y = (int) (fieldHeight * entity.getY());
				entity.render(g, x, y, fieldWidth, fieldHeight);
			}
		}
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;

		// Für ScrollPane
		this.setPreferredSize(new Dimension(this.getMap().getxSize() * GameConstants.tileXSize, this.getMap().getySize() * GameConstants.tileYSize));
		this.revalidate();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		int mx = e.getX();
		int my = e.getY();
		if (map != null) {
			int fieldWidth = GameConstants.tileXSize;
			int fieldHeight = GameConstants.tileYSize;

			if (fieldWidth > fieldHeight)
				fieldWidth = fieldHeight;
			else
				fieldHeight = fieldWidth;

			for (int x = 0; x < map.getxSize(); x++) {
				for (int y = 0; y < map.getySize(); y++) {
					int xPos = (fieldWidth * x) + 1;
					int yPos = (fieldHeight * y) + 1;
					int width = fieldWidth - 1;
					int height = fieldHeight - 1;

					if (mx >= xPos && mx <= xPos + width && my >= yPos && my <= yPos + height) {
						Object obj = editor.getCurrentSetObject();

						editor.setMapIsModified(true);

						if (obj instanceof Tile) {
							map.setField(x, y, (Tile) editor.getCurrentSetObject());
						}
						else if (obj instanceof BasicHelpEntity) {
							BasicHelpEntity entity = (BasicHelpEntity) obj;
							entity.setX(x);
							entity.setY(y);
							if (entity instanceof PlayerStartPos) {
								map.addEntity(new PlayerStartPos((PlayerStartPos) entity, map, editor.getPrototypes()));
							}
							else {
								map.addEntity(entity);
							}
						}
					}
				}
			}
			repaint();
			Logger.getLogger("").log(Level.INFO, "repaint");
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		this.mouseReleased(e);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}
}
